{% extends 'base.html' %}

{% block title %}{{ title }} - EasyTesting{% endblock %}

{% block header %}{{ title }}{% endblock %}

{% block content %}
<div class="card">
    <div class="card-body">
        <form method="post">
            {% csrf_token %}
            
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="{{ form.name.id_for_label }}" class="form-label">任务名称 *</label>
                        {{ form.name }}
                        {% if form.name.errors %}
                        <div class="text-danger small">{{ form.name.errors.0 }}</div>
                        {% endif %}
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="{{ form.test_suite.id_for_label }}" class="form-label">测试套件 *</label>
                        {{ form.test_suite }}
                        {% if form.test_suite.errors %}
                        <div class="text-danger small">{{ form.test_suite.errors.0 }}</div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <div class="mb-3">
                <label for="{{ form.description.id_for_label }}" class="form-label">任务描述</label>
                {{ form.description }}
                {% if form.description.errors %}
                <div class="text-danger small">{{ form.description.errors.0 }}</div>
                {% endif %}
            </div>

            <div class="mb-3">
                <label for="{{ form.environment.id_for_label }}" class="form-label">执行环境 *</label>
                {{ form.environment }}
                {% if form.environment.errors %}
                <div class="text-danger small">{{ form.environment.errors.0 }}</div>
                {% endif %}
            </div>

            <hr>
            <h5>调度配置</h5>

            <div class="row">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label for="{{ form.schedule_type.id_for_label }}" class="form-label">调度类型 *</label>
                        {{ form.schedule_type }}
                        {% if form.schedule_type.errors %}
                        <div class="text-danger small">{{ form.schedule_type.errors.0 }}</div>
                        {% endif %}
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3" id="scheduled_time_field">
                        <label for="{{ form.scheduled_time.id_for_label }}" class="form-label">执行时间</label>
                        {{ form.scheduled_time }}
                        {% if form.scheduled_time.errors %}
                        <div class="text-danger small">{{ form.scheduled_time.errors.0 }}</div>
                        {% endif %}
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3" id="scheduled_date_field" style="display: none;">
                        <label for="{{ form.scheduled_date.id_for_label }}" class="form-label">执行日期</label>
                        {{ form.scheduled_date }}
                        {% if form.scheduled_date.errors %}
                        <div class="text-danger small">{{ form.scheduled_date.errors.0 }}</div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3" id="weekday_field" style="display: none;">
                        <label for="{{ form.weekday.id_for_label }}" class="form-label">星期几</label>
                        {{ form.weekday }}
                        {% if form.weekday.errors %}
                        <div class="text-danger small">{{ form.weekday.errors.0 }}</div>
                        {% endif %}
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3" id="day_of_month_field" style="display: none;">
                        <label for="{{ form.day_of_month.id_for_label }}" class="form-label">每月第几天</label>
                        {{ form.day_of_month }}
                        {% if form.day_of_month.errors %}
                        <div class="text-danger small">{{ form.day_of_month.errors.0 }}</div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <div class="mb-3" id="cron_expression_field" style="display: none;">
                <label for="{{ form.cron_expression.id_for_label }}" class="form-label">Cron表达式</label>
                {{ form.cron_expression }}
                <div class="form-text">
                    格式: 分 时 日 月 周，例如: 0 9 * * 1-5 (工作日上午9点)
                    <a href="https://crontab.guru/" target="_blank">在线Cron表达式生成器</a>
                </div>
                {% if form.cron_expression.errors %}
                <div class="text-danger small">{{ form.cron_expression.errors.0 }}</div>
                {% endif %}
            </div>

            <hr>
            <h5>通知配置</h5>

            <div class="row">
                <div class="col-md-12">
                    <div class="mb-3">
                        <div class="form-check">
                            {{ form.send_email_notification }}
                            <label class="form-check-label" for="{{ form.send_email_notification.id_for_label }}">
                                发送邮件通知
                            </label>
                        </div>
                    </div>
                </div>
            </div>

            <div id="email_notification_fields">
                <div class="mb-3">
                    <label for="{{ form.notification_emails.id_for_label }}" class="form-label">通知邮箱</label>
                    {{ form.notification_emails }}
                    <div class="form-text">多个邮箱地址用逗号分隔</div>
                    {% if form.notification_emails.errors %}
                    <div class="text-danger small">{{ form.notification_emails.errors.0 }}</div>
                    {% endif %}
                </div>

                <div class="row">
                    <div class="col-md-6">
                        <div class="mb-3">
                            <div class="form-check">
                                {{ form.notify_on_success }}
                                <label class="form-check-label" for="{{ form.notify_on_success.id_for_label }}">
                                    成功时通知
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="mb-3">
                            <div class="form-check">
                                {{ form.notify_on_failure }}
                                <label class="form-check-label" for="{{ form.notify_on_failure.id_for_label }}">
                                    失败时通知
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <hr>
            <h5>重试配置</h5>

            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="{{ form.max_retries.id_for_label }}" class="form-label">最大重试次数</label>
                        {{ form.max_retries }}
                        {% if form.max_retries.errors %}
                        <div class="text-danger small">{{ form.max_retries.errors.0 }}</div>
                        {% endif %}
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="{{ form.retry_delay.id_for_label }}" class="form-label">重试间隔(秒)</label>
                        {{ form.retry_delay }}
                        {% if form.retry_delay.errors %}
                        <div class="text-danger small">{{ form.retry_delay.errors.0 }}</div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <div class="d-flex justify-content-between">
                <a href="{% url 'scheduled_task_list' %}" class="btn btn-secondary">
                    <i class="bi bi-arrow-left"></i> 返回
                </a>
                <button type="submit" class="btn btn-primary">
                    <i class="bi bi-check-lg"></i> 保存
                </button>
            </div>
        </form>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const scheduleTypeField = document.getElementById('{{ form.schedule_type.id_for_label }}');
    const scheduledTimeField = document.getElementById('scheduled_time_field');
    const scheduledDateField = document.getElementById('scheduled_date_field');
    const weekdayField = document.getElementById('weekday_field');
    const dayOfMonthField = document.getElementById('day_of_month_field');
    const cronExpressionField = document.getElementById('cron_expression_field');
    const emailNotificationCheckbox = document.getElementById('{{ form.send_email_notification.id_for_label }}');
    const emailNotificationFields = document.getElementById('email_notification_fields');

    function toggleScheduleFields() {
        const scheduleType = scheduleTypeField.value;
        
        // 隐藏所有字段
        scheduledTimeField.style.display = 'none';
        scheduledDateField.style.display = 'none';
        weekdayField.style.display = 'none';
        dayOfMonthField.style.display = 'none';
        cronExpressionField.style.display = 'none';
        
        // 根据调度类型显示相应字段
        switch(scheduleType) {
            case 'once':
                scheduledTimeField.style.display = 'block';
                scheduledDateField.style.display = 'block';
                break;
            case 'daily':
                scheduledTimeField.style.display = 'block';
                break;
            case 'weekly':
                scheduledTimeField.style.display = 'block';
                weekdayField.style.display = 'block';
                break;
            case 'monthly':
                scheduledTimeField.style.display = 'block';
                dayOfMonthField.style.display = 'block';
                break;
            case 'cron':
                cronExpressionField.style.display = 'block';
                break;
        }
    }

    function toggleEmailFields() {
        emailNotificationFields.style.display = emailNotificationCheckbox.checked ? 'block' : 'none';
    }

    scheduleTypeField.addEventListener('change', toggleScheduleFields);
    emailNotificationCheckbox.addEventListener('change', toggleEmailFields);

    // 初始化
    toggleScheduleFields();
    toggleEmailFields();
});
</script>

{% endblock %}
